<?php
// +----------------------------------------------------------------------
// | 多客怪兽框架多平台开发框架 [ 以下开发工具自动生成-版权勿删 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2021~2024 http://www.dkyx.cc All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 刘泓宾（514660094@qq.com）
// +----------------------------------------------------------------------

namespace addons\cloud_market\controller\admin;

use addons\cloud_market\model\CloudMarketQuestion;
use app\common\services\table\Column;
use app\common\services\table\DataForm;
use app\common\services\table\DataTable;
use hiduoke\controller\AddonsController;
use hiduoke\services\FormBuilder as Form;

/**
 * 问答管理控制器
 * Create by :  刘泓宾<514660094@qq.com>
 * Copyright :  copyright (c) 多客怪兽, www.dkyx.cc
 */
class Question extends AddonsController
{
    /**
     * @var CloudMarketQuestion
     */
    protected $model;

    // 设置关联查询
    protected array $withJoinTable = ['user','addon'];

    // 设置快捷搜索字段
    protected string|array $quickSearchField =  [];

    /**
     * 控制器初始化
     * Create by :  刘泓宾<514660094@qq.com>
     * @return void
     */
    public function initialize(): void
    {
        parent::initialize();
        $this->model = new CloudMarketQuestion();
    }


    /**
     * 获取数据表格结构
     * Create by :  刘泓宾<514660094@qq.com>
     * @return \think\response\Json
     */
    public function table()
    {
        $DataTable = (new DataTable($this->model));
        if ($this->request->param('recycle')) {
            // 回收站
            $DataTable->field([$this->model->getPk(), 'delete_time']);
        }
        $column = $DataTable
            // 设置 用户ID (user_id) 字段自定义配置
            ->setField("user_id",["label"=>"用户名","prop"=>"user.username","comSearchProp"=>"user_id","type"=>"remoteSelect"])
            // 设置 应用ID (addon_id) 字段自定义配置
            ->setField("addon_id",["label"=>"应用名","prop"=>"addon.title","comSearchProp"=>"addon_id","type"=>"remoteSelect"])
            // 设置 下拉多选 (label_ids) 字段自定义配置
            ->setField("label_ids",["label"=>"标签名","prop"=>"label_names","comSearchProp"=>"label_ids","type"=>"remoteSelects"])
            ->setRemoteUrls(["user_id"=>"/cloud_market/admin.user?label=username&val=id","addon_id"=>"/cloud_market/admin.addon?label=title&val=id","label_ids"=>"/cloud_market/admin.label?label=name&val=id"])
            // 默认操作列
            ->operation()
            ->create();
        return $this->success('ok', ['column' => $column, 'pk' => $this->model->getPk()]);
    }

    /**
     * 获取表单配置
     * @param $id
     * @return \think\response\Json
     * @throws \FormBuilder\Exception\FormBuilderException
     */
    public function form()
    {
        $dataForm = new DataForm($this->model);
        $form = $dataForm
            // 设置 用户ID (user_id) 字段自定义配置
            ->setField("user_id",["Title"=>"用户名","render"=>"remoteSelect"])
            // 设置 应用ID (addon_id) 字段自定义配置
            ->setField("addon_id",["Title"=>"应用名","render"=>"remoteSelect"])
            // 设置 下拉多选 (label_ids) 字段自定义配置
            ->setField("label_ids",["Title"=>"标签名","render"=>"remoteSelects"])
            ->setRemoteUrls(["user_id"=>"/cloud_market/admin.user?label=username&val=id","addon_id"=>"/cloud_market/admin.addon?label=title&val=id","label_ids"=>"/cloud_market/admin.label?label=name&val=id"])
            ->create();
        return $this->success('', $form);
    }


}
